import { defineStore } from 'pinia'

export const useBrandStore = defineStore('brand', {
  state: () => ({
    domian: '',
    documentTitle: '',
    documentIcon: '',
    title_1: '',
    logo_1: '',
    title_2: '',
    logo_2: '',
    recordNumber: '',
    copyright: '',
    description_1: '',
    description_2: '',
    xinRenShiPinFlag: '',
    download_client: '',
    kefuImage: '',
    SettingDrawerWeChatFlag: '',
    showSystemTip: '',
    promotionCenterFlag: '',
    promotionCenter_Invitation_link: '',
    promotionCenter_moneyChanger: '',
    workPlace_tipSet: '',
    workPlace_xinRenVideo: '',
    workPlace_downloadClient_url: '',
    Using_documents: '',

    SettingDrawerPhone: '',
    SettingDrawerWx: '',
    SettingDrawerQQ: '',
    SettingDrawerImage: '',
    SettingDrawerIsSetFlag: '',
    workPlace_setWeChat: '',
    workPlace_fastFlag: '',
    promotionCenter: '',
    assetsManageFlag: '',
    rightTopKfFlag: '',
    kefuText: '', //右上角官方客服 文字
    xinRenShiPinUrl:""//新人视频 链接
  }),

  actions: {
    setBrand(brand: any) {
      // console.log('pinia-brand=', brand)
      this.domian = brand.domian
      this.documentTitle = brand.documentTitle
      this.documentIcon = brand.documentIcon
      this.title_1 = brand.title_1
      this.logo_1 = brand.logo_1
      this.title_2 = brand.title_2
      this.logo_2 = brand.logo_2
      this.recordNumber = brand.recordNumber
      this.copyright = brand.copyright
      this.description_1 = brand.description_1
      this.description_2 = brand.description_2
      this.xinRenShiPinFlag = brand.xinRenShiPinFlag
      this.download_client = brand.download_client
      this.kefuImage = brand.kefuImage
      this.SettingDrawerWeChatFlag = brand.SettingDrawerWeChatFlag
      this.showSystemTip = brand.showSystemTip
      this.promotionCenterFlag = brand.promotionCenterFlag
      this.promotionCenter_Invitation_link = brand.promotionCenter_Invitation_link
      this.promotionCenter_moneyChanger = brand.promotionCenter_moneyChanger
      this.workPlace_tipSet = brand.workPlace_tipSet
      this.workPlace_downloadClient_url = brand.workPlace_downloadClient_url
      this.Using_documents = brand.Using_documents
      this.SettingDrawerPhone = brand.SettingDrawerPhone
      this.SettingDrawerWx = brand.SettingDrawerWx
      this.SettingDrawerQQ = brand.SettingDrawerQQ
      this.SettingDrawerImage = brand.SettingDrawerImage
      this.SettingDrawerIsSetFlag = brand.SettingDrawerIsSetFlag
      this.workPlace_xinRenVideo = brand.workPlace_xinRenVideo
      this.workPlace_setWeChat = brand.workPlace_setWeChat
      this.workPlace_fastFlag = brand.workPlace_fastFlag
      this.promotionCenter = brand.promotionCenter
      this.assetsManageFlag = brand.assetsManageFlag
      this.rightTopKfFlag = brand.rightTopKfFlag
      this.kefuText = brand.kefuText
      this.xinRenShiPinUrl = brand.xinRenShiPinUrl

      this.setDocumentTitle(brand.documentTitle, brand.documentIcon)
    },

    setDocumentTitle(title: string, imglog: string) {
      document.title = title
      // document.querySelector("link[rel*='icon']").href = imglog

      //
      try {
        // 使用querySelector查找所有rel属性包含'icon'的link元素
        const iconLinks = document.querySelectorAll("link[rel*='icon']")
        iconLinks.forEach(link => {
          // 更改每个找到的link元素的href属性
          link.setAttribute('href', imglog)

          // 一种更可能有效的方法是创建一个新的link元素并替换旧的（但这仍然可能不起作用）
          const newLink = document.createElement('link')
          newLink.rel = 'icon'
          newLink.href = imglog
          link.replaceWith(newLink)
        })
      } catch (error) {
        console.error('Failed to set favicon:', error)
      }
    },
  },
})
